<link rel="stylesheet" type="text/css" href="administrationpanel.css" />


<?php
// administrationpanel.php
// Start working 2013 - 02 - 25
// hladek.m

require_once('object/dictionary.ob.php');

class CategoryDictionary extends Dictionary
{
	function initproperties ()
	{
		$this->dictName = 'Categories';
		$this->columns = array(
			'id'            		=>array('ID','10','id')
			,'name'       	=>array('Name','30','')
			,'descr'       =>array('Description','40','')
			,'noart'	=>array('n.of articles','20','ne')
		);
		$this->readQuery = 'select
		category.id as id
		, category.name as name
		, category.descr as descr
		, count(article.id) as noart
		FROM category
		left join article on article.category = category.id
		group by category.id, category.name, category.descr
		order by category.name asc';
	}

	function saverow($id)
	{
		$sql = "update category set \n";
		$sql .= " name = '".$this->dictelem[$id]['name']."'\n";
		$sql .= ",descr = '".$this->dictelem[$id]['descr']."'\n";
		$sql .= "where id=".$id;
		mysql_query($sql);
	}
};

class ThreadDictionary extends Dictionary
{
	function initproperties ()
	{
		$this->dictName = 'Threads';
		$this->columns = array(
			'id'                		=>array('ID','10','id')
			,'name'         		=>array('Name','25','')
			,'descr'      	=>array('Description','35','')
			,'isblog'	=>array('Blog?','10','','checkbox')
			,'noart'   	=>array('n.of articles','20','ne')
		);
		$this->readQuery = 'select
		thread.id as id
		, thread.name as name
		, thread.descr as descr
		, thread.is_blog as isblog
		, count(thread_article.article_id) as noart
		FROM thread
		left join thread_article on thread.id = thread_article.thread_id
		group by thread.id, thread.name, thread.descr, thread.is_blog';
	}
	
	function saverow($id)
	{
		$sql = "update thread set \n";
		$sql .= " name = '".$this->dictelem[$id]['name']."'\n";
		$sql .= ",descr = '".$this->dictelem[$id]['descr']."'\n";
		$sql .= ",is_blog = '".$this->dictelem[$id]['isblog']."'\n";
		$sql .= "where id = ".$id;
		mysql_query($sql);
	}
};

class TagDictionary extends Dictionary
{
	function initproperties ()
	{
		$this->dictName = 'Tags';
		$this->columns = array(
			'id'            		=>array('ID','10','id')
			,'name'      		=>array('Name','70','')
			,'noart'	=>array('n.of articles','20','ne')
		);
		$this->readQuery = '
		select
		tag.id as id
		, tag.name as name
		, count(tag_article.id) as noart
		FROM tag
		left join tag_article on tag_article.tag_id = tag.id
		group by tag.id, tag.name
		order by tag.name';
	}
	
	function saverow($id)
	{
		$sql = "update tag set \n";
		$sql .= " name = '".$this->dictelem[$id]['name']."'\n";
		$sql .= "where id = ".$id;
		mysql_query($sql);	
	}
};


session_start();
require_once('library/string_func.lib.php');
require_once('inc/mysql_connect.inc.php');

$title     = " Administration panel";
$profileid = "12"; // temporary set on hladek.m
$html      = "<h2>Insufficient rights form administration panel</h2>";

$query     =
"SELECT count(*)
FROM profile
join role_profile on profile.id = role_profile.profile
join role on role.id = role_profile.role
where profile.id = '".$profileid."'
and role.name = 'STAFF'";

$res       = mysql_query($query);

if(1 == mysql_num_rows($res)){

	$td   = new ThreadDictionary();
	$cd   = new CategoryDictionary();
	$gd   = new TagDictionary();

	$html = "<h2>Administration panel</h2>\n";
	$html .= empty($_REQUEST)? '<h3>Data from database</h3>': '<h3>New data saved</h3>'."\n";
/*	$html .= '<p>last save '.$td->dictName.' : '.($_SESSION[$td->dictName])."\n";
	$html .= '<p>last save '.$cd->dictName.' : '.($_SESSION[$cd->dictName])."\n";
	$html .= '<p>last save '.$gd->dictName.' : '.($_SESSION[$gd->dictName])."\n";*/
	$html .= $td->show();
	$html .= $cd->show();
	$html .= $gd->show();

}

include_once("inc/view_head.inc.php");

echo $html;

include_once("inc/view_foot.inc.php");
?>